package com.sen.config;


import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//可以在链接上携带区域信息
public class MyLocalResolver implements LocaleResolver {

    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取请求中的语言参数
        String language = request.getParameter("l");
        Locale defaultLocale = Locale.getDefault(); //如果没有就使用默认的
        //如果请求的链接不为空，携带了国际化参数
        if (!StringUtils.isEmpty(language)){
            //zh_CN 字符串分割
            String[] split = language.split("_");
            //国家、地区
            Locale locale = new Locale(split[0],split[1]);
            return locale;
        }

        return defaultLocale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
